package com.ruoyi.web.controller.ring.app;

import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.YesOrNoEnums;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.Builder;
import com.ruoyi.ring.domain.RingMyReminderSet;
import com.ruoyi.ring.domain.dto.RingMyReminderSetDTO;
import com.ruoyi.ring.domain.vo.RingMyReminderSetVO;
import com.ruoyi.ring.service.IRingMyReminderSetService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 我的戒指提醒 app
 * 
 * @author liuwp
 * @date 2025-10-15
 */
@Slf4j
@RestController
@RequestMapping("/ring/app/ring_my_reminder_set")
public class AppMyReminderSetController extends BaseController
{
    @Resource
    private IRingMyReminderSetService ringMyReminderSetService;

    /**
     * 获取我的戒指提醒详细信息 app
     */
    @GetMapping("/query-reminder-set")
    public AjaxResult queryReminderSet()
    {
        RingMyReminderSetVO vo = new RingMyReminderSetVO();
        RingMyReminderSet myReminderSet = ringMyReminderSetService.selectOneRingMyReminderSet(Builder.of(RingMyReminderSet::new).with(RingMyReminderSet::setUserId, SecurityUtils.getUserId()).build());
        if(myReminderSet == null){
            vo.setInformationReminder(YesOrNoEnums.NO.getCode());
            vo.setLowBatteryAlert(YesOrNoEnums.NO.getCode());
            vo.setHeartRateWarn(YesOrNoEnums.NO.getCode());
            vo.setPlanReminder(YesOrNoEnums.NO.getCode());
        }else {
            vo = Convert.convert(RingMyReminderSetVO.class,myReminderSet);
        }
        return AjaxResult.success(vo);
    }

    /**
     * 新增我的戒指提醒 app
     */
    @Log(title = "我的戒指提醒", businessType = BusinessType.INSERT)
    @PostMapping("/operation-reminder-set")
    public AjaxResult operationReminderSet(@RequestBody RingMyReminderSetDTO dto)
    {
        long userId = SecurityUtils.getUserId();
        log.info("我的戒指提醒operationReminderSet: userId={}, param={}", userId, JSON.toJSONString(dto));
        RingMyReminderSet ringMyReminderSet = Convert.convert(RingMyReminderSet.class,dto);
        RingMyReminderSet myReminderSet = ringMyReminderSetService.selectOneRingMyReminderSet(Builder.of(RingMyReminderSet::new).with(RingMyReminderSet::setUserId, SecurityUtils.getUserId()).build());
        if(myReminderSet == null){
            ringMyReminderSet.setUserId(SecurityUtils.getUserId());
            ringMyReminderSet.setCreateTime(new Date());
            return toAjax(ringMyReminderSetService.insertRingMyReminderSet(ringMyReminderSet));
        }else {
            ringMyReminderSet.setId(myReminderSet.getId());
            return toAjax(ringMyReminderSetService.updateRingMyReminderSet(ringMyReminderSet));
        }
    }
}
